﻿// LSImmediateOffset.cs
// Contains a class for a immediate offset for a load store instruction

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace armsim
{
    /// <summary>
    /// Class of an Immediate Offset for an offset for a Load/Store instruction
    /// </summary>
    public class LSImmediateOffset : LSOffset
    {
        /// <summary>
        /// Immediate offset value
        /// </summary>
        public uint Immedidate { get; protected set; }

        
        /// <summary>
        /// Initializes a new LSImmediateOffset
        /// </summary>
        /// <param name="instruction">32bit instruction to decode</param>
        public LSImmediateOffset(uint instruction) : base(instruction)
        {
            this.Immedidate = VirtualMemory.ExtractBits(instruction, 0, 11);
        }

        /// <summary>
        /// Commputes the offset which should just be the immediate value
        /// </summary>
        /// <param name="cpu">Does nothing, but needed</param>
        /// <returns>the immediate value</returns>
        public override uint ComputeOffset(VirtualCPU cpu)
        {
            return this.Immedidate;
        }

        /// <summary>
        /// Converts the instruction to assembly
        /// </summary>
        /// <param name="cpu">the virtual cpu</param>
        /// <returns>the assembly representation</returns>
        public override string ToAssembly(VirtualCPU cpu)
        {
            return this.IsPositive
                       ? (Immedidate != 0u ? String.Format(", #{0}", Immedidate) : "")
                       : (Immedidate != 0u ? String.Format(", #-{0}", Immedidate) : "");
        }
    }
}
